﻿namespace CloudSql.Entities
{
    using System.Data.Services.Client;
    using System.Linq;
    using Microsoft.Samples.ServiceHosting.StorageClient;

        /// <summary>
    /// Azure tables service context for AuditRows
    /// </summary>
    public class AuditRowEntityDataServiceContext : TableStorageDataServiceContext
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AuditRowEntityDataServiceContext"/> class.
        /// </summary>
        /// <param name="accountInfo">
        /// Azure tables account info
        /// </param>
        public AuditRowEntityDataServiceContext(StorageAccountInfo accountInfo)
            : base(accountInfo)
        {
        }

        /// <summary>
        /// Gets all rows in azure table
        /// </summary>
        public IQueryable<AuditRowEntity> AuditRows
        {
            get
            {
                return this.CreateQuery<AuditRowEntity>("AuditRows");
            }
        }

        /// <summary>
        /// Adds row to azure table
        /// </summary>
        /// <param name="partitionKey">
        /// The partition key.
        /// </param>
        /// <param name="rowKey">
        /// The row key.
        /// </param>
        /// <param name="rowData">
        /// The row data (xml)
        /// </param>
        /// <param name="displayData">
        /// The display rowData.
        /// </param>
        public void AddAuditRowEntity(string partitionKey, string rowKey, string rowData, string displayData)
        {
            this.AddObject("AuditRows", new AuditRowEntity { PartitionKey = partitionKey, RowKey = rowKey, RowData = rowData });
            this.SaveChanges();
        }

        /// <summary>
        /// Check if row exists in Azure table
        /// </summary>
        /// <param name="rowKey">
        /// The row key.
        /// </param>
        /// <param name="partitionKey">
        /// The partition key.
        /// </param>
        /// <returns>
        /// True if exists
        /// </returns>
        public bool Exists(string rowKey, string partitionKey)
        {
            try
            {
                AuditRowEntity foundAuditRow =
                   (from auditRowEntity in this.CreateQuery<AuditRowEntity>("AuditRows")
                    where auditRowEntity.PartitionKey == partitionKey
                       && auditRowEntity.RowKey == rowKey
                    select auditRowEntity).FirstOrDefault();
            }
            catch (DataServiceQueryException ex)
            {
                return false; // Not interested in exception - assume that row does not exist
            }
            return true;
        }

        /// <summary>
        /// Returns a row (that must exist)
        /// </summary>
        /// <param name="rowKey">
        /// The row key.
        /// </param>
        /// <param name="partitionKey">
        /// The partition key.
        /// </param>
        /// <returns>
        /// Found audit row
        /// </returns>
        public AuditRowEntity GetSingleRow(string rowKey, string partitionKey)
        {
            return (from auditRowEntity in this.CreateQuery<AuditRowEntity>("AuditRows")
                where auditRowEntity.PartitionKey == partitionKey
                   && auditRowEntity.RowKey == rowKey
                select auditRowEntity).FirstOrDefault();
        }
    }
}
